package com.bnlr.lrparking.appui.bank;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.appui.paypwd.SetPayPasswordActivity;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.event.UnBindBankEvent;
import com.bnlr.lrparking.appui.bank.model.BankCardM;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 银行卡详情
 *
 * @author licy
 */
public class BankCardDetailActivity extends BaseActivity {
    private Unbinder unbinder;
    @BindView(R.id.tv_nav_title)
    TextView tvTitle;
    @BindView(R.id.bank_name)
    TextView bank_name;
    @BindView(R.id.bank_user)
    TextView bank_user;
    @BindView(R.id.bank_phone)
    TextView bank_phone;
    @BindView(R.id.unbind)
    RelativeLayout unind;
    private BankCardM.DataBean item;
    private Dialog dialog;
    private String paypassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card_detail);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("银行卡详情");
        EventBus.getDefault().register(this);
        Intent intent = getIntent();
        item = (BankCardM.DataBean) intent.getSerializableExtra("bank_detail");
        setValue(item);

        unind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view3 = View.inflate(BankCardDetailActivity.this,
                        R.layout.setting_custom_dialog, null);
                // 对话框
                final Dialog dialog2 = new AlertDialog.Builder(BankCardDetailActivity.this, R.style.NativeDialog)
                        .create();
                dialog2.show();
                dialog2.getWindow().setWindowAnimations(R.style.dialog_anim_style);
                dialog2.getWindow().setContentView(view3);
                TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
                TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
                text_phone.setText("解绑后此卡提现等功能将不可使用确认删除？");
                // 取消按钮
                TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
                btn_Cancel2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog2.dismiss();
                    }
                });
                // 确定按钮
                TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
                btn_OK2.setText("删除");
                btn_OK2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog2.dismiss();
                   startActivity(new Intent(BankCardDetailActivity.this,UnBindBankActivity.class)
                   .putExtra("bank_id",item.getId()));

                    }
                });
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        paypassword= AppConfig.getInstance().getString("paypassword", "");
        if (paypassword.equals("0")) {
            //  没有设置
            if (dialog == null) {
                showPayDialog();
            }
        }
    }
    private void showPayDialog() {
        View view3 = View.inflate(this,
                R.layout.setting_custom_dialog, null);
        // 对话框
        dialog = new AlertDialog.Builder(this, R.style.NativeDialog)
                .create();
        dialog.show();
        dialog.getWindow().setWindowAnimations(R.style.dialog_anim_style);
        dialog.getWindow().setContentView(view3);
        dialog.setCanceledOnTouchOutside(true);
        TextView tishi2 = (TextView) view3.findViewById(R.id.text_dialog);
        tishi2.setText("安全提示");
        TextView text_phone = (TextView) view3.findViewById(R.id.text_phone);
        text_phone.setText("您还没有支付密码，请先设置！");
        // 取消按钮
        TextView btn_Cancel2 = (TextView) view3.findViewById(R.id.cancle);
        btn_Cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        // 确定按钮
        TextView btn_OK2 = (TextView) view3.findViewById(R.id.sure);
        btn_OK2.setText("设置");
        btn_OK2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                startActivity(SetPayPasswordActivity.class);
                dialog = null;
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SuccessEvent(UnBindBankEvent event){
        finish();
    }
    private void setValue(BankCardM.DataBean item) {
        if (item.getBankName() != null) {
            bank_name.setText(item.getBankName() + "（尾号" + item.getAccountNo().
                    substring(item.getAccountNo().length() - 4) + "）");
        }
        bank_user.setText(item.getRealName());
        if (item.getMobile() != null && item.getMobile().length() == 11) {
            bank_phone.setText(item.getMobile().substring(0, 3) + "****" + item.getMobile()
                    .substring(item.getMobile().length() - 4));
        }
    }

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
        super.onDestroy();

    }
}
